package com.util;

import com.google.common.base.Strings;
import org.dozer.DozerConverter;
import org.dozer.converters.DateConverter;
import org.dozer.converters.DateFormatContainer;
import org.dozer.converters.StringConverter;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by frinder on 2017/5/10.
 */
public class MyDateConverter extends DozerConverter<String, Date> {

    //    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm";

    private static final DateConverter DATE_CONVERTER = new DateConverter(new SimpleDateFormat(DATE_FORMAT));

    private static final StringConverter STRING_CONVERTER = new StringConverter(new DateFormatContainer(DATE_FORMAT));

    public MyDateConverter() {
        super(String.class, Date.class);
    }

    @Override
    public Date convertTo(String s, Date date) {
        s = Strings.isNullOrEmpty(s) ? "" : s;
        return (Date) DATE_CONVERTER.convert(Date.class, s);
    }

    @Override
    public String convertFrom(Date date, String s) {
        return (String) STRING_CONVERTER.convert(String.class, date);
    }

}
